import { openDialog } from '../Dialog';
import Confirm from './components/Confirm';
import type { ModalOptions } from './types';

export const openModal = (options: ModalOptions) => {
  let loading = false;

  const {
    maskClosable = true,
    Component = Confirm,
    direction = 'center'
  } = options;

  const close = openDialog({
    duration: 300,
    maskClosable,
    type: direction,
    centerCloseZoom: .5,
    canClose: () => !loading,
    content: () => (
      <Component
        {...options}
        onLoading={(s) => loading = s}
        onBtnClose={() => !loading && close()}
      />
    )
  });
  return close;
}